{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# tensorflow2教程-基础MLP网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0-alpha0\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import tensorflow.keras as keras\n",
    "import tensorflow.keras.layers as layers\n",
    "print(tf.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.回归任务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(404, 13)   (404,)\n",
      "(102, 13)   (102,)\n"
     ]
    }
   ],
   "source": [
    "# 导入数据\n",
    "(x_train, y_train), (x_test, y_test) = keras.datasets.boston_housing.load_data()\n",
    "print(x_train.shape, ' ', y_train.shape)\n",
    "print(x_test.shape, ' ', y_test.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_10\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense_33 (Dense)             (None, 32)                448       \n",
      "_________________________________________________________________\n",
      "dense_34 (Dense)             (None, 32)                1056      \n",
      "_________________________________________________________________\n",
      "dense_35 (Dense)             (None, 32)                1056      \n",
      "_________________________________________________________________\n",
      "dense_36 (Dense)             (None, 1)                 33        \n",
      "=================================================================\n",
      "Total params: 2,593\n",
      "Trainable params: 2,593\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# 构建模型\n",
    "\n",
    "model = keras.Sequential([\n",
    "    layers.Dense(32, activation='sigmoid', input_shape=(13,)),\n",
    "    layers.Dense(32, activation='sigmoid'),\n",
    "    layers.Dense(32, activation='sigmoid'),\n",
    "    layers.Dense(1)\n",
    "])\n",
    "\n",
    "# 配置模型\n",
    "model.compile(optimizer=keras.optimizers.SGD(0.1),\n",
    "             loss='mean_squared_error',  # keras.losses.mean_squared_error\n",
    "             metrics=['mse'])\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 363 samples, validate on 41 samples\n",
      "Epoch 1/50\n",
      "363/363 [==============================] - 0s 430us/sample - loss: 371.0176 - mse: 371.0175 - val_loss: 50.0381 - val_mse: 50.0381\n",
      "Epoch 2/50\n",
      "363/363 [==============================] - 0s 28us/sample - loss: 96.3428 - mse: 96.3428 - val_loss: 51.2528 - val_mse: 51.2528\n",
      "Epoch 3/50\n",
      "363/363 [==============================] - 0s 44us/sample - loss: 93.5432 - mse: 93.5432 - val_loss: 60.6331 - val_mse: 60.6331\n",
      "Epoch 4/50\n",
      "363/363 [==============================] - 0s 45us/sample - loss: 96.2177 - mse: 96.2177 - val_loss: 88.8679 - val_mse: 88.8679\n",
      "Epoch 5/50\n",
      "363/363 [==============================] - 0s 42us/sample - loss: 93.1511 - mse: 93.1511 - val_loss: 100.1161 - val_mse: 100.1161\n",
      "Epoch 6/50\n",
      "363/363 [==============================] - 0s 35us/sample - loss: 94.3889 - mse: 94.3889 - val_loss: 43.3363 - val_mse: 43.3363\n",
      "Epoch 7/50\n",
      "363/363 [==============================] - 0s 38us/sample - loss: 90.5968 - mse: 90.5968 - val_loss: 75.2889 - val_mse: 75.2889\n",
      "Epoch 8/50\n",
      "363/363 [==============================] - 0s 34us/sample - loss: 96.9396 - mse: 96.9396 - val_loss: 42.4718 - val_mse: 42.4718\n",
      "Epoch 9/50\n",
      "363/363 [==============================] - 0s 44us/sample - loss: 90.4116 - mse: 90.4116 - val_loss: 46.8739 - val_mse: 46.8739\n",
      "Epoch 10/50\n",
      "363/363 [==============================] - 0s 32us/sample - loss: 89.8437 - mse: 89.8437 - val_loss: 68.0937 - val_mse: 68.0937\n",
      "Epoch 11/50\n",
      "363/363 [==============================] - 0s 46us/sample - loss: 92.9359 - mse: 92.9359 - val_loss: 42.6696 - val_mse: 42.6696\n",
      "Epoch 12/50\n",
      "363/363 [==============================] - 0s 35us/sample - loss: 89.6814 - mse: 89.6814 - val_loss: 46.7261 - val_mse: 46.7261\n",
      "Epoch 13/50\n",
      "363/363 [==============================] - 0s 34us/sample - loss: 90.5774 - mse: 90.5774 - val_loss: 41.6673 - val_mse: 41.6673\n",
      "Epoch 14/50\n",
      "363/363 [==============================] - 0s 44us/sample - loss: 88.5909 - mse: 88.5909 - val_loss: 55.3597 - val_mse: 55.3597\n",
      "Epoch 15/50\n",
      "363/363 [==============================] - 0s 435us/sample - loss: 90.5146 - mse: 90.5146 - val_loss: 51.8985 - val_mse: 51.8985\n",
      "Epoch 16/50\n",
      "363/363 [==============================] - 0s 43us/sample - loss: 91.7516 - mse: 91.7516 - val_loss: 43.2147 - val_mse: 43.2147\n",
      "Epoch 17/50\n",
      "363/363 [==============================] - 0s 32us/sample - loss: 91.9017 - mse: 91.9017 - val_loss: 58.9149 - val_mse: 58.9149\n",
      "Epoch 18/50\n",
      "363/363 [==============================] - 0s 36us/sample - loss: 96.3520 - mse: 96.3520 - val_loss: 39.5248 - val_mse: 39.5248\n",
      "Epoch 19/50\n",
      "363/363 [==============================] - 0s 39us/sample - loss: 84.0867 - mse: 84.0867 - val_loss: 54.0607 - val_mse: 54.0607\n",
      "Epoch 20/50\n",
      "363/363 [==============================] - 0s 38us/sample - loss: 83.8994 - mse: 83.8994 - val_loss: 100.1479 - val_mse: 100.1479\n",
      "Epoch 21/50\n",
      "363/363 [==============================] - 0s 45us/sample - loss: 101.9472 - mse: 101.9472 - val_loss: 42.8121 - val_mse: 42.8121\n",
      "Epoch 22/50\n",
      "363/363 [==============================] - 0s 41us/sample - loss: 90.9167 - mse: 90.9167 - val_loss: 53.8228 - val_mse: 53.8228\n",
      "Epoch 23/50\n",
      "363/363 [==============================] - 0s 48us/sample - loss: 91.6642 - mse: 91.6642 - val_loss: 42.9756 - val_mse: 42.9756\n",
      "Epoch 24/50\n",
      "363/363 [==============================] - 0s 45us/sample - loss: 92.6146 - mse: 92.6146 - val_loss: 76.5047 - val_mse: 76.5047\n",
      "Epoch 25/50\n",
      "363/363 [==============================] - 0s 38us/sample - loss: 94.8085 - mse: 94.8085 - val_loss: 51.9597 - val_mse: 51.9597\n",
      "Epoch 26/50\n",
      "363/363 [==============================] - 0s 33us/sample - loss: 92.2917 - mse: 92.2917 - val_loss: 42.8546 - val_mse: 42.8546\n",
      "Epoch 27/50\n",
      "363/363 [==============================] - 0s 38us/sample - loss: 90.6413 - mse: 90.6413 - val_loss: 42.8628 - val_mse: 42.8628\n",
      "Epoch 28/50\n",
      "363/363 [==============================] - 0s 38us/sample - loss: 91.5466 - mse: 91.5466 - val_loss: 42.7437 - val_mse: 42.7437\n",
      "Epoch 29/50\n",
      "363/363 [==============================] - 0s 32us/sample - loss: 93.5539 - mse: 93.5539 - val_loss: 48.5895 - val_mse: 48.5895\n",
      "Epoch 30/50\n",
      "363/363 [==============================] - 0s 33us/sample - loss: 95.7540 - mse: 95.7540 - val_loss: 43.6151 - val_mse: 43.6151\n",
      "Epoch 31/50\n",
      "363/363 [==============================] - 0s 32us/sample - loss: 89.4952 - mse: 89.4952 - val_loss: 71.6649 - val_mse: 71.6649\n",
      "Epoch 32/50\n",
      "363/363 [==============================] - 0s 48us/sample - loss: 89.5014 - mse: 89.5014 - val_loss: 44.1873 - val_mse: 44.1873\n",
      "Epoch 33/50\n",
      "363/363 [==============================] - 0s 30us/sample - loss: 93.1029 - mse: 93.1029 - val_loss: 93.6874 - val_mse: 93.6874\n",
      "Epoch 34/50\n",
      "363/363 [==============================] - 0s 53us/sample - loss: 95.2734 - mse: 95.2734 - val_loss: 45.0203 - val_mse: 45.0203\n",
      "Epoch 35/50\n",
      "363/363 [==============================] - 0s 45us/sample - loss: 90.2837 - mse: 90.2837 - val_loss: 98.1514 - val_mse: 98.1514\n",
      "Epoch 36/50\n",
      "363/363 [==============================] - 0s 40us/sample - loss: 95.9373 - mse: 95.9373 - val_loss: 44.1146 - val_mse: 44.1146\n",
      "Epoch 37/50\n",
      "363/363 [==============================] - 0s 39us/sample - loss: 91.6945 - mse: 91.6945 - val_loss: 42.3970 - val_mse: 42.3970\n",
      "Epoch 38/50\n",
      "363/363 [==============================] - 0s 45us/sample - loss: 88.5509 - mse: 88.5509 - val_loss: 45.7873 - val_mse: 45.7873\n",
      "Epoch 39/50\n",
      "363/363 [==============================] - 0s 27us/sample - loss: 93.6409 - mse: 93.6409 - val_loss: 74.0321 - val_mse: 74.0321\n",
      "Epoch 40/50\n",
      "363/363 [==============================] - 0s 23us/sample - loss: 87.0454 - mse: 87.0454 - val_loss: 37.7646 - val_mse: 37.7646\n",
      "Epoch 41/50\n",
      "363/363 [==============================] - 0s 26us/sample - loss: 92.6228 - mse: 92.6228 - val_loss: 44.1021 - val_mse: 44.1021\n",
      "Epoch 42/50\n",
      "363/363 [==============================] - 0s 40us/sample - loss: 88.6639 - mse: 88.6639 - val_loss: 32.3741 - val_mse: 32.3741\n",
      "Epoch 43/50\n",
      "363/363 [==============================] - 0s 42us/sample - loss: 92.7604 - mse: 92.7604 - val_loss: 42.6321 - val_mse: 42.6321\n",
      "Epoch 44/50\n",
      "363/363 [==============================] - 0s 43us/sample - loss: 93.3373 - mse: 93.3373 - val_loss: 60.7727 - val_mse: 60.7727\n",
      "Epoch 45/50\n",
      "363/363 [==============================] - 0s 26us/sample - loss: 94.0312 - mse: 94.0312 - val_loss: 43.1645 - val_mse: 43.1645\n",
      "Epoch 46/50\n",
      "363/363 [==============================] - 0s 31us/sample - loss: 89.4547 - mse: 89.4548 - val_loss: 42.7366 - val_mse: 42.7366\n",
      "Epoch 47/50\n",
      "363/363 [==============================] - 0s 31us/sample - loss: 89.8723 - mse: 89.8723 - val_loss: 44.8266 - val_mse: 44.8266\n",
      "Epoch 48/50\n",
      "363/363 [==============================] - 0s 34us/sample - loss: 90.3171 - mse: 90.3171 - val_loss: 47.8453 - val_mse: 47.8453\n",
      "Epoch 49/50\n",
      "363/363 [==============================] - 0s 31us/sample - loss: 86.2326 - mse: 86.2326 - val_loss: 29.5082 - val_mse: 29.5082\n",
      "Epoch 50/50\n",
      "363/363 [==============================] - 0s 28us/sample - loss: 80.1490 - mse: 80.1490 - val_loss: 30.6706 - val_mse: 30.6706\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x7fb7ec3f3e48>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 训练\n",
    "model.fit(x_train, y_train, batch_size=50, epochs=50, validation_split=0.1, verbose=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "102/102 [==============================] - 0s 116us/sample - loss: 75.0492 - mse: 75.0492\n"
     ]
    }
   ],
   "source": [
    "result = model.evaluate(x_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['loss', 'mse']\n",
      "[75.04923741957721, 75.04924]\n"
     ]
    }
   ],
   "source": [
    "print(model.metrics_names)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.分类任务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(398, 30)   (398,)\n",
      "(171, 30)   (171,)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import load_breast_cancer\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "whole_data = load_breast_cancer()\n",
    "x_data = whole_data.data\n",
    "y_data = whole_data.target\n",
    "\n",
    "x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.3, random_state=7)\n",
    "\n",
    "print(x_train.shape, ' ', y_train.shape)\n",
    "print(x_test.shape, ' ', y_test.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_14\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense_46 (Dense)             (None, 32)                992       \n",
      "_________________________________________________________________\n",
      "dense_47 (Dense)             (None, 32)                1056      \n",
      "_________________________________________________________________\n",
      "dense_48 (Dense)             (None, 1)                 33        \n",
      "=================================================================\n",
      "Total params: 2,081\n",
      "Trainable params: 2,081\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# 构建模型\n",
    "model = keras.Sequential([\n",
    "    layers.Dense(32, activation='relu', input_shape=(30,)),\n",
    "    layers.Dense(32, activation='relu'),\n",
    "    layers.Dense(1, activation='sigmoid')\n",
    "])\n",
    "\n",
    "model.compile(optimizer=keras.optimizers.Adam(),\n",
    "             loss=keras.losses.binary_crossentropy,\n",
    "             metrics=['accuracy'])\n",
    "model.summary()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "398/398 [==============================] - 0s 44us/sample - loss: 0.1982 - accuracy: 0.9196\n",
      "Epoch 2/10\n",
      "398/398 [==============================] - 0s 28us/sample - loss: 0.2094 - accuracy: 0.9221\n",
      "Epoch 3/10\n",
      "398/398 [==============================] - 0s 40us/sample - loss: 0.2128 - accuracy: 0.9246\n",
      "Epoch 4/10\n",
      "398/398 [==============================] - 0s 28us/sample - loss: 0.2101 - accuracy: 0.9271\n",
      "Epoch 5/10\n",
      "398/398 [==============================] - 0s 21us/sample - loss: 0.2175 - accuracy: 0.9146\n",
      "Epoch 6/10\n",
      "398/398 [==============================] - 0s 31us/sample - loss: 0.2925 - accuracy: 0.8945\n",
      "Epoch 7/10\n",
      "398/398 [==============================] - 0s 37us/sample - loss: 0.4531 - accuracy: 0.8618\n",
      "Epoch 8/10\n",
      "398/398 [==============================] - 0s 26us/sample - loss: 0.3105 - accuracy: 0.8920\n",
      "Epoch 9/10\n",
      "398/398 [==============================] - 0s 34us/sample - loss: 0.2934 - accuracy: 0.8794\n",
      "Epoch 10/10\n",
      "398/398 [==============================] - 0s 27us/sample - loss: 0.2597 - accuracy: 0.9045\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x7fb7da74ccc0>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(x_train, y_train, batch_size=64, epochs=10, verbose=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "171/171 [==============================] - 0s 463us/sample - loss: 0.3877 - accuracy: 0.8772\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.38765583248340596, 0.877193]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(x_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['loss', 'accuracy']\n"
     ]
    }
   ],
   "source": [
    "print(model.metrics_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
